clear all
set more off


***********************************************************
//// 03-PROCESS_CRASH: IMPORT AND SAVE RAW CRASH DATA ////
***********************************************************


***************************************************
//////// CRASH PART A: CRASH INFO FILES  /////////
**************************************************

program drop _all

***************************************************************
///// PART 1: 2006-2010 (FIRST PART) HAVE THE SAME FORMAT /////
***************************************************************

program define store1 
import delimited using `1', encoding(ISO-8859-1) clear
* REPORT NUMBER *
* DUPLICATES?? *
cap destring reportnumber, replace force
drop if mi(reportnumber)
* DATE *
gen date=date(crashdate,"MDY")
format %td date
gen year=year(date)
gen datayear=`2'
* OTHER VARS *
gen countynum=countycode
gen numvehicle=totalnumberofvehicles
gen form_type=""
gen part=1 
* KEEP *
keep reportnumber part datayear date year countynum numvehicle form_type codeable
order reportnumber part datayear date year countynum numvehicle form_type codeable
* SAVE *
qui compress
saveold "${temp}/crash_`2'", replace
end

* RUN PROGRAM ON FILES *
store1 "${raw}/crash/2006/non_redacted_2006_20061231_event_20130326.txt" 2006
store1 "${raw}/crash/2007/non_redacted_2007_20071231_event_20130325.txt" 2007
store1 "${raw}/crash/2008/non_redacted_2008_20081231_event_20130325.txt" 2008
store1 "${raw}/crash/2009/non_redacted_2009_20091231_event_20130321.txt" 2009
store1 "${raw}/crash/2010/non_redacted_2010_20101231_event_20110304.txt" 2010

* APPEND *
use "${temp}/crash_2006", clear
forval y=2007/2010 {
	qui append using "${temp}/crash_`y'"
}
* SAVE PART 1*
qui compress
saveold "${temp}/part_1", replace
* DELETE DATA FILES *
forval y=2006/2010 {
	rm "${temp}/crash_`y'.dta"
}


******************************************************************
///// PART 2: 2010 (SECOND PART) - 2014 HAVE THE SAME FORMAT /////
******************************************************************

program define store2
import delimited using `1', encoding(ISO-8859-1) clear
* REPORT NUMBER *
* DUPLICATES? *
cap destring report_number, replace force
ren report_number reportnumber
* DATE *
gen sdate=substr(crash_date_time,1,10)
gen date=date(sdate,"YMD")
format %td date
gen year=year(date)
gen datayear=`2'
* OTHER VARS *
gen countynum=county_code
gen numvehicle=total_number_of_vehicles
replace form_type=trim(upper(form_type))
gen part=2
* KEEP *
keep reportnumber part datayear date year countynum numvehicle form_type
order reportnumber part datayear date year countynum numvehicle form_type
* SAVE *
qui compress
saveold "${temp}/crash_`2'", replace
end

* RUN PROGRAM ON FILES *
store2 "${raw}/crash/2010/non_redacted_2010_20101231_event_20110914.csv" 2010
store2 "${raw}/crash/2011/non_redacted_2011_20111231_event_20121025.csv" 2011
store2 "${raw}/crash/2012/non_redacted_2012_20121231_event_20140918.csv" 2012
store2 "${raw}/crash/2013/non_redacted_2013_20131231_event_20140918.csv" 2013
store2 "${raw}/crash/2014/non_redacted_2014_20141231_event_20150306.csv" 2014

* APPEND *
use "${temp}/crash_2010", clear
forval y=2011/2014 {
	qui append using "${temp}/crash_`y'"
}
* SAVE PART 2*
qui compress
saveold "${temp}/part_2", replace
* DELETE DATA FILES *
forval y=2010/2014 {
	rm "${temp}/crash_`y'.dta"
}


***************************************************************************
///// PART 3: 2015 IS IN QUARTERLY FORMAT + ONE CHANGE FROM 2010-2014 /////
***************************************************************************

program define store3
import delimited using `1', encoding(ISO-8859-1) clear
* REPORT NUMBER *
* DUPLICATES? *
cap destring report_number, replace force
ren report_number reportnumber
* DATE *
gen sdate=substr(crash_date_time,1,10)
gen date=date(sdate,"MDY")
format %td date
gen year=year(date)
gen datayear=`2'
* OTHER VARS *
gen countynum=county_code
gen numvehicle=total_number_of_vehicles
replace form_type=trim(upper(form_type))
gen part=3
* KEEP *
keep reportnumber part datayear date year countynum numvehicle form_type
order reportnumber part datayear date year countynum numvehicle form_type
* SAVE *
qui compress
saveold "${temp}/crash_`2'", replace
end


* RUN PROGRAM ON FILES (ADJUST NAMES) *
store3 "${raw}/crash/2015/2015_Q1/event.csv" 201501
store3 "${raw}/crash/2015/2015_Q2/event.csv" 201502
store3 "${raw}/crash/2015/2015_Q3/event.csv" 201503
store3 "${raw}/crash/2015/2015_Q4/event.csv" 201504

* APPEND *
use "${temp}/crash_201501", clear
forval y=201502/201504 {
	qui append using "${temp}/crash_`y'"
}
* SAVE PART 3 *
replace datayear=2015
qui compress
saveold "${temp}/part_3", replace

* DELETE DATA FILES *
forval y=201501/201504 {
	rm "${temp}/crash_`y'.dta"
}


***************************************************************************
///// PART 4: 2016 IS IN QUARTERLY FORMAT + ONE CHANGE FROM 2010-2014 /////
***************************************************************************

* RUN PROGRAM ON FILES (ADJUST NAMES) *
store3 "${raw}/crash/2016/2016_Q1/event.csv" 201601
store3 "${raw}/crash/2016/2016_Q2/event.csv" 201602
store3 "${raw}/crash/2016/2016_Q3/event.csv" 201603
store3 "${raw}/crash/2016/2016_Q4/event.csv" 201604

* APPEND *
use "${temp}/crash_201601", clear
forval y=201602/201604 {
	qui append using "${temp}/crash_`y'"
}
* SAVE PART 3 *
replace datayear=2016
qui compress
saveold "${temp}/part_4", replace

* DELETE DATA FILES *
forval y=201601/201604 {
	rm "${temp}/crash_`y'.dta"
}



**********************************************************************
///// PART 5: 2017-2018 SEEM TO FOLLOW SAME PATTERN AS 2010-2014 /////
**********************************************************************

store2 "${raw}/crash/2017/non_redacted_2017_20171231_A-event_20190926.csv" 2017
store2 "${raw}/crash/2018/non_redacted_2018_20181231_event_20190830.csv" 2018


* APPEND *
use "${temp}/crash_2017", clear
qui append using "${temp}/crash_2018"

* SAVE PART 2*
qui compress
saveold "${temp}/part_5", replace
* DELETE DATA FILES *
forval y=2017/2018 {
	rm "${temp}/crash_`y'.dta"
}



*********************************
///// PART 6: ALL TOGETHER /////
*********************************

use "${temp}/part_1", clear
qui append using "${temp}/part_2"
qui append using "${temp}/part_3"
qui append using "${temp}/part_4"
qui append using "${temp}/part_5"
qui compress

///// DEAL WITH DUPLICATES //////
duplicates drop

* DROP OBSERVATIONS WHERE THERE IS A "GOOD ONE" WITH THE SAME REPORTNUMBER *
gen good=0
replace good=1 if form_type=="L"
replace good=1 if codeable==0 & part==1
bysort reportnumber: egen anygood=max(good)
drop if anygood==1&good==0

* DROP DUPLICATES WITH NO GOOD OBSERVATIONS *
duplicates tag reportnumber, gen(tag)
drop if tag>0 & anygood==0
drop tag anygood good

* DROP REMAINING DUPLICATES *
* NOTE: ONLY 187 ENTRIES *
duplicates tag reportnumber, gen(tag)
drop if tag>0
drop tag


* STORE! *
qui compress
saveold "${temp}/CRASH-INFO", replace


* DELETE TEMP FILES *
forval i=1/5 {
rm "${temp}/part_`i'.dta"
}





***********************************************
//////// CRASH PART B: DRIVER FILES  /////////
***********************************************


program drop _all

**************************************************
///// PART 1: 2006-2010 HAVE THE SAME FORMAT /////
**************************************************

program define store1
import delimited using `1', encoding(ISO-8859-1) clear
* REPORT NUMBER *
cap destring reportnumber, replace force
drop if mi(reportnumber)
* FLORIDA DRIVERS ONLY *
replace dlstate=trim(upper(dlstate))
keep if dlstate=="FL"
replace dlnumber=trim(upper(dlnumber))
keep if length(dlnumber)==13
* OTHER VARS *
gen datayear=`2'
gen part=1
* ORGANIZE *
keep reportnumber part datayear dlnumber driveraction
order reportnumber part datayear dlnumber driveraction
* STORE *
qui compress
saveold "${temp}/driver_`2'", replace
end

* RUN PROGRAM ON DATA FILES *
store1 "${raw}/crash/2006/non_redacted_2006_20061231_driver_20130326.txt" 2006
store1 "${raw}/crash/2007/non_redacted_2007_20071231_driver_20130325.txt" 2007
store1 "${raw}/crash/2008/non_redacted_2008_20081231_driver_20130325.txt" 2008
store1 "${raw}/crash/2009/non_redacted_2009_20091231_driver_20130321.txt" 2009
store1 "${raw}/crash/2010/non_redacted_2010_20101231_driver_20110304.txt" 2010

* COMPILE INTO PART 1 *
use "${temp}/driver_2006", clear
forval y=2007/2010 {
	qui append using "${temp}/driver_`y'"
}
* STORE *
qui compress
saveold "${temp}/part_1", replace
* DELETE *
forval y=2006/2010 {
	rm "${temp}/driver_`y'.dta"
}


**************************************************
///// PART 2: 2011-2014 HAVE THE SAME FORMAT /////
**************************************************

program define store2
import delimited using `1', encoding(ISO-8859-1) clear
* REPORT NUMBER *
ren report_number reportnumber
cap destring reportnumber, replace force
drop if mi(reportnumber)
* FLORIDA DRIVERS ONLY *
ren driver_license_state dlstate
ren dl_number dlnumber
replace dlstate=trim(upper(dlstate))
keep if dlstate=="FL"
replace dlnumber=trim(upper(dlnumber))
keep if length(dlnumber)==13
* OTHER VARS *
gen datayear=`2'
gen part=2
ren driver_action_1 driveraction
* ORGANIZE *
keep reportnumber part datayear dlnumber driveraction
order reportnumber part datayear dlnumber driveraction
* STORE *
qui compress
saveold "${temp}/driver_`2'", replace
end

* RUN PROGRAM ON DATA FILES *
store2 "${raw}/crash/2011/non_redacted_2011_20111231_driver_20121025.csv" 2011
store2 "${raw}/crash/2012/non_redacted_2012_20121231_driver_20140918.csv" 2012
store2 "${raw}/crash/2013/non_redacted_2013_20131231_driver_20140918.csv" 2013
store2 "${raw}/crash/2014/non_redacted_2014_20141231_driver_20150306.csv" 2014

* COMPILE INTO PART 1 *
use "${temp}/driver_2011", clear
forval y=2012/2014 {
	qui append using "${temp}/driver_`y'"
}
* STORE *
qui compress
saveold "${temp}/part_2", replace
* DELETE *
forval y=2011/2014 {
	rm "${temp}/driver_`y'.dta"
}


********************************************
///// PART 3: 2015 IN QUARTERLY FORMAT /////
********************************************

* RUN PROGRAM ON DATA FILES *
store2 "${raw}/crash/2015/2015_Q1/driver.csv" 201501
store2 "${raw}/crash/2015/2015_Q2/driver.csv" 201502
store2 "${raw}/crash/2015/2015_Q3/driver.csv" 201503
store2 "${raw}/crash/2015/2015_Q4/driver.csv" 201504

* COMPILE INTO PART 1 *
use "${temp}/driver_201501", clear
forval y=201502/201504 {
	qui append using "${temp}/driver_`y'"
}
* STORE *
qui compress
replace datayear=2015
saveold "${temp}/part_3", replace
* DELETE *
forval y=201501/201504 {
	rm "${temp}/driver_`y'.dta"
}


********************************************
///// PART 4: 2016 IN QUARTERLY FORMAT /////
********************************************

* RUN PROGRAM ON DATA FILES *
store2 "${raw}/crash/2016/2016_Q1/driver.csv" 201601
store2 "${raw}/crash/2016/2016_Q2/driver.csv" 201602
store2 "${raw}/crash/2016/2016_Q3/driver.csv" 201603
store2 "${raw}/crash/2016/2016_Q4/driver.csv" 201604

* COMPILE INTO PART 1 *
use "${temp}/driver_201601", clear
forval y=201602/201604 {
	qui append using "${temp}/driver_`y'"
}
* STORE *
qui compress
replace datayear=2016
saveold "${temp}/part_4", replace

* DELETE *
forval y=201601/201604 {
	rm "${temp}/driver_`y'.dta"
}


***********************************************************
///// PART 5: 2017-2018 HAVE SAME FORMAT AS 2010-2014 /////
***********************************************************

* RUN PROGRAM ON DATA FILES *
store2 "${raw}/crash/2017/non_redacted_2017_20171231_A-driver_20190926.csv" 2017
store2 "${raw}/crash/2018/non_redacted_2018_20181231_driver_20190830.csv" 2018

* COMPILE INTO PART 5 *
use "${temp}/driver_2017", clear
qui append using "${temp}/driver_2018"

* STORE *
qui compress
saveold "${temp}/part_5", replace
* DELETE *
forval y=2017/2018 {
	rm "${temp}/driver_`y'.dta"
}



********************************
///// PART 6: ALL TOGETHER /////
********************************

use "${temp}/part_1", clear
qui append using "${temp}/part_2"
qui append using "${temp}/part_3"
qui append using "${temp}/part_4"
qui append using "${temp}/part_5"
qui compress
saveold "${temp}/CRASH-DRIVER", replace

* DELETE TEMP FILES *
forval i=1/5 {
	rm "${temp}/part_`i'.dta"
}







***********************************************
//////// CRASH PART C: INJURY FILES /////////
***********************************************

program drop _all

***************************************************************
//// INTERMEDIATE STEP: TEMPORARY FILE FOR 2010 PASSENGERS ////
//// THIS IS SO THE PART 1 PROGRAM WILL WORK FOR 2010      ////
***************************************************************

import delim using "${raw}/crash/2010/non_redacted_2010_20101231_passenger_20110914.csv", encoding(ISO-8859-1) clear
ren report_number reportnumber
ren injury_severity injuryseverity
keep reportnumber injuryseverity
tempfile ADD
save `ADD'

import delim using "${raw}/crash/2010/non_redacted_2010_20101231_passenger_20110304.txt", encoding(ISO-8859-1) clear
keep reportnumber injuryseverity
qui append using `ADD'
export delimited ${raw}/crash/2010/TEMP_passenger_2010.csv, replace



**************************************************
///// PART 1: 2006-2010 HAVE THE SAME FORMAT /////
**************************************************

program define store1

//// PART 1: DRIVER FILE /////
import delimited using `1', encoding(ISO-8859-1) clear
* REPORT NUMBER *
cap destring reportnumber, replace force
drop if mi(reportnumber)
* TAKE WORST INJURY SEVERITY *
replace injuryseverity=0 if mi(injuryseverity)
replace injuryseverity=5 if !mi(injuryseverity)&(injuryseverity>5)
collapse (max) injuryseverity, by(reportnumber)
ren injuryseverity driver_injury
* STORE *
qui compress
saveold "${temp}/TEMP1", replace

/// PART 2: PASSENGER FILE ///
import delimited using `2', encoding(ISO-8859-1) clear
* REPORT NUMBER *
cap destring reportnumber, replace force
drop if mi(reportnumber)
* TAKE WORST INJURY SEVERITY *
replace injuryseverity=0 if mi(injuryseverity)
replace injuryseverity=5 if !mi(injuryseverity)&(injuryseverity>5)
collapse (max) injuryseverity, by(reportnumber)
ren injuryseverity pass_injury
* STORE *
qui compress
saveold "${temp}/TEMP2", replace

/// PART 3: PEDESTRIAN FILE ////
import delimited using `3', encoding(ISO-8859-1) clear
* REPORT NUMBER *
cap destring reportnumber, replace force
drop if mi(reportnumber)
* TAKE WORST INJURY SEVERITY *
replace injuryseverity=0 if mi(injuryseverity)
replace injuryseverity=5 if !mi(injuryseverity)&(injuryseverity>5)
collapse (max) injuryseverity, by(reportnumber)
ren injuryseverity ped_injury
* STORE *
qui compress
saveold "${temp}/TEMP3", replace

/// PUT TOGETHER ///
use "${temp}/TEMP1", clear
merge 1:1 reportnumber using "${temp}/TEMP2", nogen
merge 1:1 reportnumber using "${temp}/TEMP3", nogen
* STORE *
gen datayear = `4'
qui compress
saveold "${temp}/injury_`4'", replace
* DELETE TEMP FILES *
forval i=1/3 {
	rm "${temp}/TEMP`i'.dta"
}
* END OF PROGRAM *
end


* RUN PROGRAM ON DATA FILES *
#delimit ;
store1 "${raw}/crash/2006/non_redacted_2006_20061231_driver_20130326.txt" 
	   "${raw}/crash/2006/non_redacted_2006_20061231_passenger_20130326.txt"
	   "${raw}/crash/2006/non_redacted_2006_20061231_pedestrian_20130326.txt"
	    2006 ;
store1 "${raw}/crash/2007/non_redacted_2007_20071231_driver_20130325.txt" 
	   "${raw}/crash/2007/non_redacted_2007_20071231_passenger_20130325.txt"
	   "${raw}/crash/2007/non_redacted_2007_20071231_pedestrian_20130325.txt"
	    2007 ;
store1 "${raw}/crash/2008/non_redacted_2008_20081231_driver_20130325.txt"
	   "${raw}/crash/2008/non_redacted_2008_20081231_passenger_20130325.txt"
	   "${raw}/crash/2008/non_redacted_2008_20081231_pedestrian_20130325.txt"
	    2008 ;
store1 "${raw}/crash/2009/non_redacted_2009_20091231_driver_20130321.txt"
	   "${raw}/crash/2009/non_redacted_2009_20091231_passenger_20130321.txt"
	   "${raw}/crash/2009/non_redacted_2009_20091231_pedestrian_20130321.txt"
	    2009 ;
store1 "${raw}/crash/2010/non_redacted_2010_20101231_driver_20110304.txt"
	   "${raw}/crash/2010/TEMP_passenger_2010.csv"
	   "${raw}/crash/2010/non_redacted_2010_20101231_pedestrian_20110304.txt"
	    2010 ;
#delimit cr
* COMPILE INTO PART 1 *
use "${temp}/injury_2006", clear
forval y=2007/2010 {
	qui append using "${temp}/injury_`y'"
}
* STORE *
qui compress
saveold "${temp}/part_1", replace
* DELETE *
forval y=2006/2010 {
	rm "${temp}/injury_`y'.dta"
}
*** DELETE TEMPORARY 2010 SPREADSHEET ***
rm "${raw}/crash/2010/TEMP_passenger_2010.csv"



**************************************************
///// PART 2: 2011-2014 HAVE THE SAME FORMAT /////
**************************************************

program define store2

/// PART 1: DRIVER ////
import delimited using `1', encoding(ISO-8859-1) clear
* REPORT NUMBER *
ren report_number reportnumber
cap destring reportnumber, replace force
drop if mi(reportnumber)
* TAKE WORST INJURY SEVERITY *
ren injury_severity injuryseverity
replace injuryseverity=0 if mi(injuryseverity)
replace injuryseverity=5 if !mi(injuryseverity)&(injuryseverity>5)
collapse (max) injuryseverity, by(reportnumber)
ren injuryseverity driver_injury
* STORE *
qui compress
saveold "${temp}/TEMP1", replace

/// PART 2: PASSENGER ///
import delimited using `2', encoding(ISO-8859-1) clear
* REPORT NUMBER *
ren report_number reportnumber
cap destring reportnumber, replace force
drop if mi(reportnumber)
* TAKE WORST INJURY SEVERITY *
ren injury_severity injuryseverity
replace injuryseverity=0 if mi(injuryseverity)
replace injuryseverity=5 if !mi(injuryseverity)&(injuryseverity>5)
collapse (max) injuryseverity, by(reportnumber)
ren injuryseverity pass_injury
* STORE *
qui compress
saveold "${temp}/TEMP2", replace

/// PART 2: PEDESTRIAN ///
import delimited using `3', encoding(ISO-8859-1) clear
* REPORT NUMBER *
ren report_number reportnumber
cap destring reportnumber, replace force
drop if mi(reportnumber)
* TAKE WORST INJURY SEVERITY *
ren injury_severity injuryseverity
replace injuryseverity=0 if mi(injuryseverity)
replace injuryseverity=5 if !mi(injuryseverity)&(injuryseverity>5)
collapse (max) injuryseverity, by(reportnumber)
ren injuryseverity ped_injury
* STORE *
qui compress
saveold "${temp}/TEMP3", replace

/// PUT TOGETHER ///
use "${temp}/TEMP1", clear
merge 1:1 reportnumber using "${temp}/TEMP2", nogen
merge 1:1 reportnumber using "${temp}/TEMP3", nogen
* STORE *
gen datayear = `4'
qui compress
saveold "${temp}/injury_`4'", replace
* DELETE TEMP FILES *
forval i=1/3 {
	rm "${temp}/TEMP`i'.dta"
}
* END OF PROGRAM *
end



* RUN PROGRAM ON DATA FILES *
#delimit ;
store2 "${raw}/crash/2011/non_redacted_2011_20111231_driver_20121025.csv" 
	   "${raw}/crash/2011/non_redacted_2011_20111231_passenger_20121025.csv"
	   "${raw}/crash/2011/non_redacted_2011_20111231_non_motorist_20121025.csv"
	    2011 ;
store2 "${raw}/crash/2012/non_redacted_2012_20121231_driver_20140918.csv" 
	   "${raw}/crash/2012/non_redacted_2012_20121231_passenger_20140918.csv"
	   "${raw}/crash/2012/non_redacted_2012_20121231_non_motorist_20140918.csv"
	    2012 ;
store2 "${raw}/crash/2013/non_redacted_2013_20131231_driver_20140918.csv"
	   "${raw}/crash/2013/non_redacted_2013_20131231_passenger_20140918.csv"
	   "${raw}/crash/2013/non_redacted_2013_20131231_non_motorist_20140918.csv"
	    2013 ;
store2 "${raw}/crash/2014/non_redacted_2014_20141231_driver_20150306.csv"
	   "${raw}/crash/2014/non_redacted_2014_20141231_passenger_20150306.csv"
	   "${raw}/crash/2014/non_redacted_2014_20141231_non_motorist_20150306.csv"
	    2014 ;
#delimit cr

* COMPILE INTO PART 2 *
use "${temp}/injury_2011", clear
forval y=2012/2014 {
	qui append using "${temp}/injury_`y'"
}
* STORE *
qui compress
saveold "${temp}/part_2", replace
* DELETE *
forval y=2011/2014 {
	rm "${temp}/injury_`y'.dta"
}


********************************************
///// PART 3: 2015 IN QUARTERLY FORMAT /////
********************************************

#delimit ;
store2 "${raw}/crash/2015/2015_Q1/driver.csv" 
	   "${raw}/crash/2015/2015_Q1/passenger.csv"
	   "${raw}/crash/2015/2015_Q1/non_motorist.csv"
	    201501 ;
store2 "${raw}/crash/2015/2015_Q2/driver.csv" 
	   "${raw}/crash/2015/2015_Q2/passenger.csv"
	   "${raw}/crash/2015/2015_Q2/non_motorist.csv"
	    201502 ;
store2 "${raw}/crash/2015/2015_Q3/driver.csv" 
	   "${raw}/crash/2015/2015_Q3/passenger.csv"
	   "${raw}/crash/2015/2015_Q3/non_motorist.csv"
	    201503 ;
store2 "${raw}/crash/2015/2015_Q4/driver.csv" 
	   "${raw}/crash/2015/2015_Q4/passenger.csv"
	   "${raw}/crash/2015/2015_Q4/non_motorist.csv"
	    201504 ;
#delimit cr

* COMPILE INTO PART 1 *
use "${temp}/injury_201501", clear
forval y=201502/201504 {
	qui append using "${temp}/injury_`y'"
}
* STORE *
qui compress
replace datayear=2015
saveold "${temp}/part_3", replace
* DELETE *
forval y=201501/201504 {
	rm "${temp}/injury_`y'.dta"
}


********************************************
///// PART 4: 2016 IN QUARTERLY FORMAT /////
********************************************

#delimit ;
store2 "${raw}/crash/2016/2016_Q1/driver.csv" 
	   "${raw}/crash/2016/2016_Q1/passenger.csv"
	   "${raw}/crash/2016/2016_Q1/non_motorist.csv"
	    201601 ;
store2 "${raw}/crash/2016/2016_Q2/driver.csv" 
	   "${raw}/crash/2016/2016_Q2/passenger.csv"
	   "${raw}/crash/2016/2016_Q2/non_motorist.csv"
	    201602 ;
store2 "${raw}/crash/2016/2016_Q3/driver.csv" 
	   "${raw}/crash/2016/2016_Q3/passenger.csv"
	   "${raw}/crash/2016/2016_Q3/non_motorist.csv"
	    201603 ;
store2 "${raw}/crash/2016/2016_Q4/driver.csv" 
	   "${raw}/crash/2016/2016_Q4/passenger.csv"
	   "${raw}/crash/2016/2016_Q4/non_motorist.csv"
	    201604 ;
#delimit cr

* COMPILE INTO PART 1 *
use "${temp}/injury_201601", clear
forval y=201602/201604 {
	qui append using "${temp}/injury_`y'"
}
* STORE *
qui compress
replace datayear=2016
saveold "${temp}/part_4", replace

* DELETE *
forval y=201601/201604 {
	rm "${temp}/injury_`y'.dta"
}


******************************************************
///// PART 5: 2017-2018 SAME FORMAT AS 2010-2014 /////
******************************************************

#delimit ;
store2 "${raw}/crash/2017/non_redacted_2017_20171231_A-driver_20190926.csv" 
	   "${raw}/crash/2017/non_redacted_2017_20171231_A-passenger_20190926.csv" 
	   "${raw}/crash/2017/non_redacted_2017_20171231_A-non_motorist_20190926.csv" 
	    2017 ;
store2 "${raw}/crash/2018/non_redacted_2018_20181231_driver_20190830.csv" 
	   "${raw}/crash/2018/non_redacted_2018_20181231_passenger_20190830.csv" 
	   "${raw}/crash/2018/non_redacted_2018_20181231_non_motorist_20190830.csv" 
	    2018 ;
#delimit cr


* COMPILE INTO PART 5 *
use "${temp}/injury_2017", clear
qui append using "${temp}/injury_2018"
* STORE *
qui compress
saveold "${temp}/part_5", replace
* DELETE *
forval y=2017/2018 {
	rm "${temp}/injury_`y'.dta"
}




************************************
///// PART 6: ALL TOGETHER NOW /////
************************************ 

use "${temp}/part_1", clear
qui append using "${temp}/part_2"
qui append using "${temp}/part_3"
qui append using "${temp}/part_4"
qui append using "${temp}/part_5"

qui compress
saveold "${temp}/CRASH-INJURY", replace

* DELETE TEMP FILES *
forval i=1/5 {
	rm "${temp}/part_`i'.dta"
}





***********************************************
//////// CONSTRUCT MASTER CRASH FILE /////////
//////// DATASET IS AT DRIVER LEVEL  /////////
***********************************************


use "${temp}/CRASH-INJURY", clear
egen severity=rowmax(driver_injury pass_injury ped_injury)
collapse (max) severity, by(reportnumber)
tempfile INJURY
save `INJURY'

use "${temp}/CRASH-DRIVER", clear
merge m:1 reportnumber using "${temp}/CRASH-INFO", keep(3) nogen
merge m:1 reportnumber using `INJURY', 

drop datayear year
gen multicar = (numvehicle>1)
drop numvehicle

order dlnumber date reportnumber countynum form_type codeable part driveraction severity multicar
label var dlnumber "DL Number"
label var date "Date"
label var reportnumber "Crash ID"
label var countynum "County"
label var form_type "Form Type"
label var codeable "Codeable?"
label var part "Data Part"
label var driveraction "Driver Action"
label var severity "Max Injury Severity"
label var multicar "Multiple Vehicles"

qui compress
saveold "${temp}/process-crash", replace 

rm "${temp}/CRASH-INFO.dta"
rm "${temp}/CRASH-DRIVER.dta"
rm "${temp}/CRASH-INJURY.dta"












